/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.handler;

import net.cellobject.javacodeformatter.model.FormatterManager;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.ui.part.ResourceTransfer;

/**
 * Paste the current contents of the clipboard into the Favorites view by adding those objects to the collection of Favorites
 * objects.
 */
public class PasteFormatterHandler extends ClipboardHandler {
	/**
	 * Called by the superclass {@link #execute(ExecutionEvent)} method so that this method can concentrate on the operation and
	 * does not have to manage clipboard creation and disposal.
	 */
	@Override
	protected Object execute(final ExecutionEvent event, final Clipboard clipboard) throws ExecutionException {
		paste(clipboard, ResourceTransfer.getInstance());
		paste(clipboard, JavaUI.getJavaElementClipboardTransfer());
		return null;
	}

	private void paste(final Clipboard clipboard, final Transfer transfer) {
		final Object[] elements = (Object[])clipboard.getContents(transfer);
		if ( (elements != null) && (elements.length != 0)) {
			FormatterManager.getManager().addFavorites(elements);
		}
	}
}
